package com.auto.learning.adapter;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.SearchBean;
import com.auto.learning.view.MyListView;

public class SearchComprehensiveListAdapter extends BaseAdapter {

	private Context context;
	private SearchBean searchBean;
	private Handler handler;
	private Map<Integer, String> map = new HashMap<Integer, String>();
	
	
	public SearchComprehensiveListAdapter(Context context,SearchBean searchBean,Handler handler){
		this.context = context;
		this.searchBean = searchBean;
		this.handler = handler;
	}
	
	@Override
	public int getCount() {
		map.clear();
		int count = 0;
		if (searchBean.getBookModel() != null && searchBean.getBookModel().size() > 0) {
			map.put(count, "s");
			count ++;
		}
		if (searchBean.getAnchorModel() != null && searchBean.getAnchorModel().size() > 0) {
			map.put(count, "z");
			count ++;
		}
		if (searchBean.getHofModel() != null && searchBean.getHofModel().size() > 0) {
			map.put(count, "m");
			count ++;
		}
		return map.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	ViewHolder holder = null;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.search_comprehensive_adapter, null);
			holder = new ViewHolder();
			holder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
			holder.tv_title_type = (TextView) convertView.findViewById(R.id.tv_title_type);
			holder.myListView = (MyListView) convertView.findViewById(R.id.listview_del);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		switch (map.get(position)) {
			case "m":
				holder.tv_title_type.setText("大咖秀");
				holder.tv_more.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						handler.obtainMessage(1, "m").sendToTarget();
					}
				});
				VideoPageAdapter videoPageAdapter = new VideoPageAdapter(context, searchBean.getHofModel() , null, 1);
				holder.myListView.setAdapter(videoPageAdapter);
				break;
			case "s":
				holder.tv_title_type.setText("书籍");
				holder.tv_more.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						handler.obtainMessage(1, "s").sendToTarget();
					}
				});
				HomePageListViewAdapter homePageListViewAdapter = new HomePageListViewAdapter(context, searchBean.getBookModel());
				holder.myListView.setAdapter(homePageListViewAdapter);
				break;
			case "z":
				holder.tv_title_type.setText("主播");
				holder.tv_more.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						handler.obtainMessage(1, "z").sendToTarget();
					}
				});
				AnchorAdapter anchorAdapter = new AnchorAdapter(context, searchBean.getAnchorModel());
				holder.myListView.setAdapter(anchorAdapter);
				break;
		}
		return convertView;
	}
	
	class ViewHolder {
		TextView tv_title_type,tv_more;
		MyListView myListView;
	}
}
